﻿@using WebAPP_LIULI.Models;
@{
    Order m = Model;
    if (m == null)
    {
        m = new Order();
    }

    User user = (User)Session["User"];
}

<!-- Page-Title -->
<div class="row">
    <div class="col-sm-12">

        <h4 class="page-title">订单录入</h4>
    </div>
</div>
<!-- end page title end breadcrumb -->


<div class="row">

    <div class="col-12">
        <div class="card-box">
            <form id="FormBody" class="form-horizontal" role="form" action="/Home/OrderInput" method="post">
                <input hidden="hidden" type="text" name="Id" value="@m.Id">
                <div class="form-group row">
                    <label class="col-md-2 col-form-label">选择顾客</label>
                    <div class="col-md-10">
                        <select id="Customer" class="form-control" name="Customer.Id">
                            @foreach (var c in ViewBag.customers)
                            {
                                <option value="@c.Id" @if (m.Customer != null && c.Id == m.Customer.Id) { <text> selected</text> }>@c.CustomerName</option>
                            }
                        </select>
                    </div>
                </div>

                <dl class="row">
                    <dt class="col-6">顾客名称</dt>
                    <dd id="CustomerName" class="col-6">
                        @if (m.Customer != null)
                        {@m.Customer.CustomerName}
                    </dd>
                    <dt class="col-6">顾客地址</dt>
                    <dd id="CustomerAddress" class="col-6">
                        @if (m.Customer != null)
                        {@m.Customer.CustomerAddress}
                    </dd>
                    <dt class="col-6">联系人</dt>
                    <dd id="Contact" class="col-6">
                        @if (m.Customer != null)
                        {@m.Customer.Contact}
                    </dd>
                    <dt class="col-6">联系电话</dt>
                    <dd id="ContactPhone" class="col-6">
                        @if (m.Customer != null)
                        {@m.Customer.ContactPhone}
                    </dd>
                    <dt class="col-6">订单发货量</dt>
                    <dd class="col-6">@m.DeliveryCount</dd>
                </dl>


                <div class="form-group row">
                    <label class="col-md-2 col-form-label">选择产品</label>
                    <div class="col-md-10">
                        <select class="form-control" name="ProductName">
                            @foreach (var c in ViewBag.products)
                            {
                                <option value="@c.ProductName" @if (c.ProductName == m.ProductName) { <text> selected</text> }>@c.ProductName</option>
                            }
                        </select>
                    </div>
                </div>

                <div class="form-group row">
                    <label class="col-md-2 col-form-label">订单数量/吨</label>
                    <div class="col-md-10">
                        <input class="form-control" type="number" name="ProductCount" value="@m.ProductCount">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-md-2 col-form-label">订单金额</label>
                    <div class="col-md-10">
                        <input class="form-control" type="number" name="OrderPrice" value="@m.OrderPrice">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-md-2 col-form-label">交货日期</label>
                    <div class="col-md-10">
                        <input class="form-control" type="date" name="DeliveryTime" value="@m.DeliveryTime">
                    </div>
                </div>

                <div class="form-group row">
                    <label class="col-md-2 col-form-label">订单状态</label>
                    <div class="col-md-10">
                        <select id="Customer" class="form-control" name="OrderStatus">
                            <option value="已下单" @if (m.OrderStatus == "已下单") { <text> selected </text> }>已下单</option>
                            <option value="部分发货" @if (m.OrderStatus == "部分发货") { <text> selected </text> }>部分发货</option>
                            <option value="已完成" @if (m.OrderStatus == "已完成") { <text> selected </text> }>已完成</option>
                            <option value="已关闭" @if (m.OrderStatus == "已关闭") { <text> selected </text> }>已关闭</option>
                        </select>
                    </div>
                </div>

                <div class="form-group row">
                    <label class="col-md-2 col-form-label">备注</label>
                    <div class="col-md-10">
                        <textarea class="form-control" name="Remarks" id="Remarks" rows="2">@m.Remarks</textarea>
                    </div>
                </div>

                <input hidden="hidden" type="text" name="Salesman" value="@if (string.IsNullOrEmpty(m.Salesman)) { @user.UserName } else { @m.Salesman; }">
                <input hidden="hidden" type="text" name="TaskId" value="@if (string.IsNullOrEmpty(m.TaskId)) { @("Order_" + DateTime.Now.ToString("yyyyMMddHHmmss"))  } else { @m.TaskId; }">

                <dl class="row p-t-10">
                    <dt class="col-6">订单号</dt>
                    <dd class="col-6">
                        @if (string.IsNullOrEmpty(m.TaskId))
                        {@("Order_" + DateTime.Now.ToString("yyyyMMddHHmmss")) }
                    else
                    { @m.TaskId;
                }
                </dd>
                <dt class="col-6">业务员</dt>
                <dd class="col-6">
                    @if (string.IsNullOrEmpty(m.Salesman))
                    {@user.UserName }
                else
                {@m.Salesman;
            }
                </dd>
            </dl>
            <button type="button" class="btn btn-block btn-xs btn-purple waves-effect waves-light" id="formsubmit">提交</button>
            </form>


        </div>
    </div><!-- end col -->

    <script>
        $(document).ready(function () {
            $('#formsubmit').click(function () {
                swal({
                    title: '确定提交么？',
                    text: "请确定好填写内容正确无误后提交！",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#4fa7f3',
                    cancelButtonColor: '#d57171',
                    confirmButtonText: '提交',
                    cancelButtonText: '取消'
                }).then(function () {
                    swal(
                        '提交成功！',
                        '您已成功提交！',
                        'success'
                    )
                    $('#FormBody').submit();

                }, function (dismiss) {
                    // dismiss can be 'cancel', 'overlay',
                    // 'close', and 'timer'
                    if (dismiss === 'cancel') {
                        swal(
                            '已取消',
                            '请核对信息后再次提交！',
                            'error'
                        )
                    }
                })
            });

            getCustomerInfo();

            $('#Customer').change(function () {
                getCustomerInfo();
            })

            function getCustomerInfo() {
                var checkValue = $("#Customer").val();
                    $.post('/Home/CustomerDetailJson', { id: checkValue }, function (data) {
                        var o = JSON.parse(data)
                        $('#CustomerName').html(o.CustomerName);
                        $('#CustomerAddress').html(o.CustomerAddress);
                        $('#Contact').html(o.Contact);
                        $('#ContactPhone').html(o.ContactPhone);
                    });
            }
        });

        
    </script>

</div>
<!-- end row -->